package com.sf.fy.uav.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sf.fy.uav.common.awt.JwtUtil;
import com.sf.fy.uav.common.awt.RepeatSubmit;
import com.sf.fy.uav.common.enums.OrderStatusEnum;
import com.sf.fy.uav.common.request.common.IdRequest;
import com.sf.fy.uav.common.request.operate.OrderInfoReq;
import com.sf.fy.uav.common.request.order.OrderInfoAddRequest;
import com.sf.fy.uav.common.response.operational.OrderInfoRes;
import com.sf.fy.uav.common.response.order.BillingResponse;
import com.sf.fy.uav.common.response.order.OrderResponse;
import com.sf.fy.uav.common.response.order.task.DistributionOrderListRes;
import com.sf.fy.uav.common.response.order.task.ScenicSpotOrderDetailRes;
import com.sf.fy.uav.common.utils.RespJsonPageData;
import com.sf.fy.uav.order.entity.OrderInfo;
import com.sf.fy.uav.order.mapper.OrderInfoMapper;
import com.sf.fy.uav.order.service.IOrderInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LTQ
 * @since 2023-05-08
 */
@Controller
@RequestMapping("/sys/orderInfo")
public class OrderInfoRest {

    @Autowired
    private IOrderInfoService iOrderInfoService;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    private final static Integer CONST_NUM = 4;
    private final static Integer TO_BE_PAID = -1;

    private final static Integer WAITING_FOR_ORDER_ACCEPTANCE = 0;

    private final static Integer MATERIAL_PREPARATION = 1;

    private final static Integer IN_TRANSIT = 2;

    @RepeatSubmit(interval = 2000, message = "重复提交")
    @ApiOperation(value = "取消订单")
    public Date cancelOrder(@RequestBody @Valid IdRequest idRequest) {
        return iOrderInfoService.cancelOrder(idRequest.getId());
    }

    public void add(OrderInfoAddRequest orderInfoAddRequest) {
        iOrderInfoService.add(orderInfoAddRequest);
    }

    public List<OrderResponse> getSendOrder(@RequestParam Integer page, @RequestParam Integer size) {
        return iOrderInfoService.getSendOrder(page, size);
    }

    public List<OrderInfo> getOrders(@RequestParam Integer page, @RequestParam Integer size) {
        return iOrderInfoService.getOrders(page, size);
    }

    public Long getSendOrderTotal() {
        return iOrderInfoService.getSendOrderTotal();
    }

    public Long getReceiveOrderTotal(String phoneNumber) {
        return iOrderInfoService.getReceiveOrderTotal(phoneNumber);
    }


    public List<OrderResponse> getReceiveOrder(String phone, Integer page, Integer size) {
        return iOrderInfoService.getReceiveOrder(phone, page, size);
    }

    public Long getTotal(@RequestParam(required = false) String tcWaybillNo, @RequestParam(required = false) String tradeNo, @RequestParam(required = false) Integer type, @RequestParam(required = false) Integer platformCode, @RequestParam(required = false) String enterpriseId, @RequestParam(required = false) Integer payStatus, @RequestParam(required = false) Date startTransactionTime, @RequestParam(required = false) Date endTransactionTime) {
        return iOrderInfoService.getTotal(tcWaybillNo, tradeNo, type, platformCode, enterpriseId, payStatus, startTransactionTime, endTransactionTime);
    }

    public List<BillingResponse> getBillings(@RequestParam Integer page, @RequestParam Integer size, @RequestParam String tcWaybillNo, @RequestParam String tradeNo, @RequestParam Integer type, @RequestParam Integer platformCode, @RequestParam(required = false) String enterpriseId, @RequestParam(required = false) Integer payStatus, Date startTransactionTime, Date endTransactionTime) {
        return iOrderInfoService.getBillings(page, size, tcWaybillNo, tradeNo, type, platformCode, enterpriseId, payStatus, startTransactionTime, endTransactionTime);
    }

    public List<OrderInfo> getOrderInfoListByUserIds(@RequestParam Set<String> useridSet) {
        return iOrderInfoService.selectList(new QueryWrapper<OrderInfo>().eq("order_status", CONST_NUM).in("user_id", useridSet));
    }

    public RespJsonPageData<OrderInfoRes> getScenicSpotOrder(OrderInfoReq orderInfoReq) {
        return iOrderInfoService.getScenicSpotOrder(orderInfoReq);
    }

    public ScenicSpotOrderDetailRes getScenicSpotOrderDetail(String tcWaybillNo) {
        return iOrderInfoService.getScenicSpotOrderDetail(tcWaybillNo);
    }

    public List<OrderInfo> getOrderInfoListInTcWaybillNos(Set<String> tcWaybillNos) {
        return iOrderInfoService.list(new QueryWrapper<OrderInfo>().and(wrapper -> wrapper.eq("order_status", TO_BE_PAID).or().eq("order_status", WAITING_FOR_ORDER_ACCEPTANCE).or().eq("order_status", MATERIAL_PREPARATION).or().eq("order_status", IN_TRANSIT).or().eq("order_status", CONST_NUM)).in("tc_waybill_no", tcWaybillNos));
    }

    public RespJsonPageData<DistributionOrderListRes> getOrderList(OrderInfoReq orderInfoReq) {
        return iOrderInfoService.queryOrderList(orderInfoReq);
    }

    public List<OrderInfo> getOrderInfoListByInUserCouponIds(Set<String> userCouponIdSet) {
        return iOrderInfoService.list(new QueryWrapper<OrderInfo>().eq("order_status", CONST_NUM).in("user_coupon_id", userCouponIdSet));
    }

    public OrderInfo getOrderInfoByTcWaybillNoAndPlatformCode(String tcWaybillNo, Integer platformCode) {
        QueryWrapper<OrderInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("tc_waybill_no", tcWaybillNo);
        if (!ObjectUtils.isEmpty(platformCode)) {
            wrapper.eq("platform_code", platformCode);
        }
        return iOrderInfoService.selectOne(wrapper);
    }


    public List<OrderInfo> getOrdersInTcWaybillNos(Set<String> tcWaybillNos) {
        return iOrderInfoService.list(new QueryWrapper<OrderInfo>().in("tc_waybill_no", tcWaybillNos));
    }


    public Long selectFinishOrder(String userId) {
        QueryWrapper<OrderInfo> orderInfoQueryWrapper = new QueryWrapper<>();
        orderInfoQueryWrapper.eq("order_status", OrderStatusEnum.COMPLETED.getStatus());
        orderInfoQueryWrapper.eq("user_id", userId);
        return orderInfoMapper.selectCount(orderInfoQueryWrapper);
    }

}